package com.hc.acoustics.widget.banner;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;

import com.yc.yclibx.file.YcImgUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Banner适配器
 */
public class YcBannerAdapterNew extends PagerAdapter {
    private List<YcBannerBeanNew> mBannerData = new ArrayList<>();
    private List<ImageView> mImageViewList = new ArrayList<>();
    private ViewPagerOnItemClickListener mViewPagerOnItemClickListener;
    private Context mContext;

    YcBannerAdapterNew(List<YcBannerBeanNew> list, Context context) {
        this.mBannerData = list;
        this.mContext = context;
        for (YcBannerBeanNew banner : mBannerData) {
            ImageView mImageView = new ImageView(context);
            mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            switch (banner.getImgType()) {
                case YcBannerBeanNew.ImgType.LOCATION:
                    YcImgUtils.loadNetImg(context, banner.getImgPath(), mImageView);
                    break;
                case YcBannerBeanNew.ImgType.NET:
                    YcImgUtils.loadNetImg(context, banner.getImgUrl(), mImageView);
                    break;
                case YcBannerBeanNew.ImgType.RES:
                    mImageView.setImageResource(banner.getResId());
                    break;
            }
            mImageViewList.add(mImageView);
        }
        if (mBannerData.size() == 2) {
            for (int i = 0; i < 2; i++) {
                ImageView mImageView = new ImageView(context);
                mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

                mImageViewList.add(mImageView);
            }
        }
    }

    void setViewPagerOnItemClickListener(ViewPagerOnItemClickListener mViewPagerOnItemClickListener) {
        this.mViewPagerOnItemClickListener = mViewPagerOnItemClickListener;
    }

    @Override
    public int getCount() {
        //当图片大于1张时候才可以轮播
        if (mBannerData.size() > 1)
            return Integer.MAX_VALUE;
        else
            return mBannerData.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //对ViewPager页号求模取出View列表中要显示的项
        if (position < 0) {
            position = mImageViewList.size() + position;
        }
        position %= mImageViewList.size();
        ImageView imageView = mImageViewList.get(position);
        YcBannerBeanNew item = mBannerData.get(position);
        if (item.isLoadedImg()) {
            switch (mBannerData.get(position).getImgType()) {
                case YcBannerBeanNew.ImgType.LOCATION:
                    YcImgUtils.loadNetImg(mContext, item.getImgPath(), imageView);
                    break;
                case YcBannerBeanNew.ImgType.NET:
                    YcImgUtils.loadNetImg(mContext, item.getImgUrl(), imageView);
                    break;
                case YcBannerBeanNew.ImgType.RES:
                    YcImgUtils.loadResIdImg(mContext, item.getResId(), imageView);
                    break;
            }
        }
        //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
        ViewParent vp = imageView.getParent();
        if (vp != null) {
            ViewGroup parent = (ViewGroup) vp;
            parent.removeView(imageView);
        }
        imageView.setTag(position);
        imageView.setOnClickListener(v1 -> {
            if (mViewPagerOnItemClickListener != null) {
                int position1 = (int) v1.getTag();
                mViewPagerOnItemClickListener.onItemClick(mBannerData.get(position1 % mBannerData.size()), position1);
            }
        });
        container.addView(imageView);
        return imageView;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }


    public interface ViewPagerOnItemClickListener {
        /**
         * 轮播图点击事件
         */
        void onItemClick(YcBannerBeanNew item, int position);
    }
}
